// pages/merchant/index.js

const WXAPI = require('../../wxapi/main')

Page({

  /**
   * 页面的初始数据
   */
  data: {

    mid: 0,
    cid: 0,
    did: 0,
    scrollId: '',
    // 店铺信息
    merchant: [
      {
        mid: 0,
        uid: 1,
        name: "快餐店",
        mTradCertPic: "",
	      mDesc:"这是一家快餐店",
	      mClass:""
      },
      {
        mid: 1,
	      uid: 1,
        name: "面店",
        mTradCertPic: "",
	      mDesc:"这是一家面店",
	      mClass:""
      },
      {
        mid: 2,
        uid: 3,
        name:"冷菜点",
        mTradCertPic: "",
	      mDesc:"这是一家冷菜点",
	      mClass:""
      },
      {
        mid: 3,
        uid: 4,
        name:"烘焙店",
        mTradCertPic: "",
	      mDesc:"这是一家烘焙店",
	      mClass:""
      }
    ],

    // 菜单
    categories: [
      // 店铺1
      [{
        cid:0,
        cName:"菜",
        scrollId:''
      },
      {
        cid:1,
        cName:"汤",
        scrollId:''
      },
      {
        cid:2,
        cName:"米饭",
        scrollId:''
      }],
      // 店铺2
      [{
        cid:0,
        cName:"面",
        scrollId:''
      },
      {
        cid:1,
        cName:"饮料",
        scrollId:''
      }],
      // 店铺3
      [{
        cid:0,
        cName:"荤菜",
        scrollId:''
      },{
        cid:1,
        cName:"素菜",
        scrollId:''
      }],
      // 店铺4
      [{
        cid:0,
        cName:"面包",
        scrollId:''
      },{
        cid:1,
        cName:"披萨",
        scrollId:''
      }]
    ],
    dish: [
    [[{
      did:0,
      mid:0,
      cid:0,
      dName:"麻婆豆腐",
      dPrice:10,
      dImage:"/images/merchants/merchant1/category1/df.png",
    },{
      did:1,
      mid:0,
      cid:0,
      dName:"红烧冬瓜",
      dPrice:9,
      dImage:"/images/merchants/merchant1/category1/dg.png",
    },{
      did:2,
      mid:0,
      cid:0,
      dName:"手撕杏鲍菇",
      dPrice:9,
      dImage:"/images/merchants/merchant1/category1/xbg.png",
    }]
    ,[{
      did:10,
      mid:0,
      cid:1,
      dName:"甜艾滚蛋",
      dPrice:5,
      dImage:"/images/merchants/merchant1/category2/a.png",
    },{
      did:11,
      mid:0,
      cid:1,
      dName:"虾仁菌菇汤",
      dPrice:5,
      dImage:"/images/merchants/merchant1/category2/xr.jpg",
    }],
    [{
      did:20,
      mid:0,
      cid:2,
      dName:"米饭",
      dPrice:1,
      dImage:"/images/merchants/merchant1/category3/mf.png",
    }]],
    [[{
      did:100,
      mid:1,
      cid:0,
      dName:"臊子面",
      dPrice:10,
      dImage:"/images/merchants/merchant2/category1/sz.jpg",
    },
    {
      did:101,
      mid:1,
      cid:0,
      dName:"西红柿鸡蛋面",
      dPrice:10,
      dImage:"/images/merchants/merchant2/category1/xhs.jpg",
    },{
      did:102,
      mid:1,
      cid:0,
      dName:"意大利面",
      dPrice:10,
      dImage:"/images/merchants/merchant2/category1/ydl.jpg",
    }],
    [{
      did:110,
      mid:1,
      cid:1,
      dName:"酸梅汤",
      dPrice:7,
      dImage:"/images/merchants/merchant2/category2/sm.jpg",
    },
    {
      did:111,
      mid:1,
      cid:1,
      dName:"西瓜汁",
      dPrice:7,
      dImage:"/images/merchants/merchant2/category2/xg.jpg",
    }]],

    [[{
      did:200,
      mid:2,
      cid:0,
      dName:"手撕鸡肉",
      dPrice:16,
      dImage:"/images/merchants/merchant3/category1/jr.jpg",
    },{
      did:201,
      mid:2,
      cid:0,
      dName:"牛肉拌香菜",
      dPrice:19,
      dImage:"/images/merchants/merchant3/category1/nr.jpg",
    },{
      did:202,
      mid:2,
      cid:0,
      dName:"酱肘子",
      dPrice:20,
      dImage:"/images/merchants/merchant3/category1/zj.jpg",
    }],
    [{
      did:210,
      mid:2,
      cid:1,
      dName:"小葱拌豆腐",
      dPrice:8,
      dImage:"/images/merchants/merchant3/category2/df.jpg",
    },{
      did:211,
      mid:2,
      cid:1,
      dName:"酸辣凉粉",
      dPrice:12,
      dImage:"/images/merchants/merchant3/category2/lf.jpg",
    }]],
    [[{
      did:300,
      mid:3,
      cid:0,
      dName:"吐司",
      dPrice:10,
      dImage:"/images/merchants/merchant4/category1/ts.jpg",
    },{
      did:301,
      mid:3,
      cid:0,
      dName:"枣沙面包",
      dPrice:10,
      dImage:"/images/merchants/merchant4/category1/zs.jpg",
    }],
    [{
      did:310,
      mid:3,
      cid:1,
      dName:"水果披萨",
      dPrice:15,
      dImage:"/images/merchants/merchant4/category2/sg.jpg",
    },{
      did:311,
      mid:3,
      cid:1,
      dName:"吐司披萨",
      dPrice:15,
      dImage:"/images/merchants/merchant4/category2/ts.jpg",
    }]]],

    goodsWrap: [],
    categoriesTmp:[],
    categorySelected: "",
    goodsToView: "",
    categoryToView: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(e) {
    this.initData(e);
  },
  initData(e) {
    let that = this;
    wx.showNavigationBarLoading();

    let mid = e.mid;

    
   

    // 获取categories（根据店铺id）(默认第一个仓库)
    // that.getCategories(this.mid);

    var categories = that.data.categories;
    var categoriesTmp = that.data.categoriesTmp;

    console.log(categories);

    for (var i = 0; i < categories[mid].length; i++) {
        // 第i仓库的对象
          let item = categories[mid][i];

          item.scrollId = "s" + item.cid;
          categoriesTmp.push(item);

          if (i == 0) {
            that.setData({
              categorySelected: item.scrollId,
            })

      }
    };

    that.setData({
       mid: mid,
       categoriesTmp: categoriesTmp
    });

    that.getGoodsList(0);
    // 获取categories（wxapi）
   /*
    WXAPI.goodsCategory().then(function(res) {
      var categories = [];
      if (res.code == 0) {
        for (var i = 0; i < res.data.length; i++) {

          let item = res.data[i];

          item.scrollId = "s" + item.id;
          categories.push(item);

          if (i == 0) {
            that.setData({
              categorySelected: item.scrollId,
            })

          }
        }
      }

      that.setData({
        // categories: categories,
      });
      console.log(categories);
      that.getGoodsList(0);
    }).catch((e) => {

      wx.hideNavigationBarLoading();
    });
*/
  },
  // 店铺信息


  // 菜单操作
  getCategories: function(mid) {

  },

  // 
  getGoodsList: function(categoryId, append) {
    let that = this;
    let goodsWrap = []; //相当于categories
    let mid = that.data.mid;
    let dish = that.data.dish;

    that.data.categoriesTmp.forEach((o, index) => {
      // o:当前category对象
      // index：当前categories位置

      console.log("o:", o);
      console.log("index:",index);

      let wrap = {};
      wrap.cid = o.cid;
      wrap.scrollId = "s" + o.cid;
      wrap.cName = o.cName;
      let goods = [];

      wrap.goods = goods;
      // 每个wrap装对应的item（dish）
      dish[mid][wrap.cid].forEach((item, i) => {
        // 符合当前的位置
        console.log("item:",item);
        console.log("i",i);

        console.log("item[i]", item[i]);

          goods.push(item);
      })

      goodsWrap.push(wrap);
    })

    that.setData({
      goodsWrap: goodsWrap,
    });

    console.log("goodsWrap:",goodsWrap);

    wx.hideNavigationBarLoading();
  
  },


  toDetailsTap: function(e) {
    console.log("e.currentTarget.dataset.id",e.currentTarget.dataset.id),
    wx.navigateTo({
      

      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
    })
  },

  onCategoryClick: function(e) {

    let scrollId = e.currentTarget.dataset.id;
    console.log("srollid", scrollId);
    this.categoryClick = true;
    this.setData({
      goodsToView: scrollId,
      categorySelected: scrollId,
    })
  },
  
  scroll: function(e) {

    if (this.categoryClick){
      this.categoryClick = false;
      return;
    }

    let scrollTop = e.detail.scrollTop;

    // console.log("scroll scrollTop", scrollTop)

    let that = this;

    let offset = 0;
    let isBreak = false;

    for (let g = 0; g < this.data.goodsWrap.length; g++) {

      let goodWrap = this.data.goodsWrap[g];

      offset += 30;

      if (scrollTop <= offset) {

        if (this.data.categoryToView != goodWrap.scrollId) {
          this.setData({
            categorySelected: goodWrap.scrollId,
            categoryToView: goodWrap.scrollId,
          })
        }

        break;
      }

      for (let i = 0; i < goodWrap.goods.length; i++) {

        offset += 91;

        if (scrollTop <= offset) {

          if (this.data.categoryToView != goodWrap.scrollId) {
            this.setData({
              categorySelected: goodWrap.scrollId,
              categoryToView: goodWrap.scrollId,
            })
          }

          isBreak = true;
          break;
        }
      }

      if (isBreak){
        break;
      }


    }

  
  }
})
